#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
int n, ans;

struct node {
	int l, r;
}tree[N];

void dfs(int id,int depth) {
	if (id == 0) return;
	ans = max(ans, depth);
	dfs(tree[id].l, depth + 1);
	dfs(tree[id].r, depth + 1);
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> tree[i].l >> tree[i].r;
	}
	dfs(1,1);
	cout << ans << '\n';
	return 0;
}